<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script>
        let source = null;
        let userId = 7777
        if (window.EventSource) {
            // 建立连接
            source = new EventSource('http://localhost:8001/sse/sub/');
            setMessageInnerHTML("连接用户=" + userId);
            /**
             * 连接一旦建立，就会触发open事件
             * 另一种写法：source.onopen = function (event) {}
             */
            source.addEventListener('open', function (e) {
                setMessageInnerHTML("建立连接。。。");
            }, false);
            /**
             * 客户端收到服务器发来的数据
             * 另一种写法：source.onmessage = function (event) {}
             */
            source.addEventListener('message', function (e) {
                setMessageInnerHTML(e.data);
            });
        } else {
            setMessageInnerHTML("你的浏览器不支持SSE");
        }

        function setMessageInnerHTML(t) {
            $('body').append(`<div>${t}</div>`)
        }
    </script>

</head>
<body>

</body>
</html>